from django import newforms as forms
from apps.core.models.article import Element
from apps.core.backend.views.forms import element_form
from apps.stat.models.stat import Stat

class StatForm(element_form.ElementForm):
    url = forms.CharField(label="Url")
    identifier = None

    def __init__(self, *args, **kwargs):
        super(StatForm, self).__init__(*args, **kwargs)
        try:
            self.identifier = kwargs['initial']['id']
        except:
            pass
        self.fields["status"].initial = 2
        self.fields["order"].initial = 0
        
    def save(self, *args, **kwargs):
        if self.identifier:
            s = Stat.objects.get(related_element__id=self.identifier)
        else:
            s = Stat()

        s.url = self.cleaned_data["url"]
        s.related_element = super(StatForm, self).save(tp="stat:Stat", *args, **kwargs)
        s.save()
        return




